package com.yitao.cms.config.permission;

import com.skipper.base.bean.DataOutput;
import com.yitao.cms.exception.AuthException;
import com.yitao.cms.exception.YiTaoCmsRuntimeException;
import com.yitao.cms.utils.BaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@ControllerAdvice(basePackages = "com.yitao.cms.controller")
public class ExceptionControllerAdvice {


    private BaseUtils base = BaseUtils.getInstance();

    protected final Logger logger = LoggerFactory.getLogger(ExceptionControllerAdvice.class);

    /**
     * 权限异常拦截
     *
     * @param ex
     * @return
     */
   /*@ExceptionHandler(AuthException.class)
    public DataOutput<String> authExceptionHandler(Exception ex) {
        return resultAuthException(ex);
    }

    private <T extends Throwable> DataOutput<String> resultAuthException(T ex) {
        logger.error("controller 请求无权限，全局返回{}", ex.getMessage());
        return new DataOutput();
    }
*/
    @ExceptionHandler(YiTaoCmsRuntimeException.class)
    public DataOutput<String> yitaoCmsRuntimeExceptionHandler(Exception ex) {
        return yitaoCmsRuntimeExceptionException(ex);
    }

    private <T extends Throwable> DataOutput<String> yitaoCmsRuntimeExceptionException(T ex) {
        return base.getErrorMsg(ex.getMessage());
    }

}
